function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()


  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function makeDateTimeToPretty(date, onlyMinute) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()
  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()
  
  var dateStr = month + '月' + day + '日';
  var dayRange = '上午';

  if (hour > 12) {
    hour = hour - 12;
    dayRange = '下午';
  }

  if (hour < 10) {
    hour = '0' + hour;
  }

  if (minute < 10) {
    minute = '0' + minute;
  }

  if (second < 10) {
    second = '0' + second;
  }

  return dateStr + ' ' + dayRange + hour + ':' + minute + (onlyMinute ? '' : (':' + second));
}

function makeDateTimeToPrettyNoneDetail(date, onlyMinute) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var dateStr = month + '月' + day + '日';

  return dateStr;
}

function buildSelfPath(self, options) {
  var path = self.__route__ + '?';

  if (options) {
    for (var key in options) {
      if (typeof options[key] !== 'Object') {
        path += key + '=' + options[key] + '&';
      }
    }
  }

  return path.substring(0, path.length - 1);
}

function checkPermission(self, options) {
  var app = getApp();

  var referer = buildSelfPath(self, options);

  if (app.globalData.apiUser && app.globalData.apiToken) {
    return true;
  } else {
    wx.redirectTo({
      url: '/pages/register/register?referer=' + encodeURIComponent(referer)
      //url:'/pages/dataset/dataset'
    });
    return false;
  }
}

function buildPaySignature(appId, nonceStr, pack, signType, timeStamp) {
  var md5 = require("md5.min");
  var str = 'appId=' + appId + '&nonceStr=' + nonceStr + '&package=' +  pack + '&signType=' + signType + '&timeStamp=' + timeStamp + '&key=' + getApp().globalData.mchSecret;
  return md5(str).toUpperCase();
}

function formatDate(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  return [year, month, day].map(formatNumber).join('-');
}

function formatDateTime(date) {
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var day = date.getDate();
  var hour = date.getHours();
  var minute = date.getMinutes() + 1;
  var second = date.getSeconds();

  return [year, month, day].map(formatNumber).join('-') + ' '
    + [hour, minute, second].map(formatNumber).join(':');
}

function formatDateStr(date) {
  var segs = date.split(' ')[0].split('/');
  if (segs.length < 3) {
    segs = date.split(' ')[0].split('-');
  }
  var year = segs[0];
  var month = segs[1];
  var day = segs[2];

  return [year, month, day].map(formatNumber).join('-');
}

function getDateTimestamp(shortDate, millseconds) {
  var segs = shortDate.split('-');
  var year = parseInt(segs[0]);
  var month = parseInt(segs[1]) - 1;
  var day = parseInt(segs[2]);
  var date = new Date();

  date.setDate(day);
  date.setMonth(month);
  date.setFullYear(year);

  if (millseconds) {
    return date.getTime();
  }
  return parseInt(date.getTime() / 1000);
}

function signature(data) {
  return data;
}

function makeNonceStr() {
  var hhex = parseInt(Math.random() * 100000000);
  var lhex = parseInt(Math.random() * 100000000);

  return hhex + '' + lhex;
}

function processFailureRequest(failureCallback, res, statusCode) {
  if (failureCallback) {
    failureCallback.call(this, {
      message: res.data.Message,
      errorCode: res.data.ErrorCode,
    });
  } else {
    wx.showToast({
      title: res.data.ErrorCode,
      icon: 'loading',
      duration: 2000
    });
  }
}

function buildRequestheader() {
  var tokenHelper = require('tokenHelper.js');

  return Object.assign({ 'content-type': 'application/json', 'X-Requested-With': 'XMLHttpRequest'}, tokenHelper.getToken);
}

module.exports = {
  makeDateTimeToPrettyNoneDetail: makeDateTimeToPrettyNoneDetail,
  formatTime: formatTime,
  formatDate: formatDate,
  formatDateStr: formatDateStr,
  formatDateTime: formatDateTime,
  getDateTimestamp,getDateTimestamp,
  checkPermission: checkPermission,
  buildPaySignature: buildPaySignature,
  signature: signature,
  buildSelfPath: buildSelfPath,
  processFailureRequest: processFailureRequest,
  buildRequestheader: buildRequestheader,
  makeDateTimeToPretty: makeDateTimeToPretty
}